package com.atjava.webspider.html;

import java.io.File;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.atjava.webspider.util.HttpUtil;

public class TestRun {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		TestRun run = new TestRun();
		run.run1();
	}
	
	private void run(){
		String url = "http://dev.21tx.com/2004/11/05/11263.html";
		//String url = "http://jindong:8080/WaterLicence/index.action";
		try {
			HttpURLConnection connection = HttpUtil.getValidConnection(new URL(url));
			//HttpUtil.writeDataResource(connection, new File("c:/123.txt"));
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	private void run1(){
		try{
			String url = "http://dev.21tx.com/2004/11/05/11263.html";
			//String url = "http://jindong:8080/WaterLicence/index.action";
			HttpURLConnection connection = HttpUtil.getValidConnection(new URL(url));
			if(connection != null){
				String content = HttpUtil.getTextResource(connection,"gb2312");
				//connection.disconnect();
				if(content!=null && content.length() > 0){
					LinkParser parser = DefaultLinkParser.getInstance();
					List<String> links = parser.parseLink(content);
					//if(links!=null){
						for(int i=0;i<links.size();i++){
							//LinkTags tag = links.get(i);
							//System.out.println("target="+tag.getTarget()+",href="+tag.getHref()+",name="+tag.getName());
							System.out.println(links.get(i));
						}
						System.out.println(links.size());
					}
					/*List<ImageTags> images = parser.getImageTags();
					if(images!=null){						
						for(int i=0;i<images.size();i++){
							ImageTags tag = images.get(i);
							//System.out.println("src="+tag.getSrc()+",width="+tag.getWidth()+",height="+tag.getHeight());
							System.out.println("src="+tag.getSrc());
						}
					}*/
//					List<Task> tskList = new TaskManager().loadKindTasks("java");
//					Analysis ans = new Analysis(links,tskList.get(0));
//					ans.contextualURL();
				}
			//}else{
				///System.out.println("The URL["+url+"] is disconnection  ");
			//}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	private void run2(){
		try{
//			String content="";
//			String url = "http://dev.21tx.com/2004/02/14/18997.html";
//			HttpURLConnection connection = WebspiderUtil.getValidConnection(new URL(url));
//			if(connection != null){
//				 content = WebspiderUtil.downloadStringResource(connection);
//			}
//			
			//String content = "<a title=\"11\" HREF=http://www.atjava.com target=\"_balnk\" >atjava</a>";
			//content+="<a class=11 target='_blank' href =\"http://www.bbb.com\">bbsbbsbbs</a>";
			
			String content = "http://dev.21tx.com/2004/11/05/11263.html";
			
			String imgcontent = "<img src=http://www.atjava.com  width=488> asdf<img src=\"http://www.bb.com\">";
			imgcontent+="<img class=11 target='_blank' src =\"http://www.bbb.com\"/>";
			
			//String imgPattern1 = "<(?i)img\\s[^>]*(?i)src\\s*=\\s*\"([^\"]*)\"\\s*[^>]*>";
			//String urlPattern = "<(?i)a\\s[^>]*(?i)href\\s*=\\s*([^\"'][^>]*)>(.*?)</(?i)a>";
			//String imgPattern = "<(?i)img\\s[^>]*(?i)src\\s*=([^\"'][^>]*)>";
			//String pagePattern = "</span><p align=right>(.*?)</p>";
			String urlp = "dev.21tx.com/[0-9]{4}/[0-9]{2}/[0-9]{2}/[^/]*.html";
			
			Pattern p = Pattern.compile(urlp);
			//System.out.println(this.content);
			Matcher m = p.matcher(content);
			while(m.find()){
			//	System.out.println(m.group(1));
				for(int i=0;i<=m.groupCount();i++){
					System.out.println(m.group(i));
				}
			}
//			if(m.find()){
//				System.out.println(m.group(0)+"\n\n");
//				System.out.println(m.group(1)+"\n\n");
//				
//				//System.out.println(getPage(m.group(1)));
//			}
			
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	private String getPage(String content){
		
		StringBuffer sb = new StringBuffer();
		
		// get links
		String urlPattern = "<(?i)a\\s[^>]*(?i)href\\s*=\\s*(.*)([^>]|[^\\s])";
		Pattern up = Pattern.compile(urlPattern);
		Matcher um = up.matcher(content);
		
		while(um.find()){
			if(um.groupCount() == 2){
				sb.append(um.group(1));
				sb.append(",");
			}
		}
		String result = sb.toString();
		if(result.length() > 0){
			result = result.substring(0, result.length()-1);
		}
		return result;
	}

}
